package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.UserDailyLuckRecordDao;
import com.jbp.model.UserDailyLuckRecord;
import com.jbp.service.UserDailyLuckRecordService;
import com.jbp.utils.StrToMillsUtil;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.math.BigDecimal;
import java.util.*;

/**
 * 每日用户抽奖统计(UserDailyLuckRecord)表服务实现类
 * @author cc
 * @since 2020-08-23 14:32:08
 */
@Service
public class UserDailyLuckRecordServiceImpl extends ServiceImpl<UserDailyLuckRecordDao, UserDailyLuckRecord> implements UserDailyLuckRecordService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<UserDailyLuckRecord> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null){
            queryWrapper.like("id",params.get("id"));
        }
        IPage<UserDailyLuckRecord> page = this.page(
                new Query<UserDailyLuckRecord>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public BigDecimal getPayToday(Integer userId) {
        return getBaseMapper().getPayToday(userId);
    }

    @Override
    public double getPayToday(Integer userId, String poolType) {
        BigDecimal payToday = getBaseMapper().getPayToday2(userId, poolType);
        return payToday == null ? 0 : payToday.doubleValue();
    }

    @Override
    public HashMap<String, Object> getLuckToday(Integer userId, String poolType) {
        return getBaseMapper().getLuckToday(userId, poolType);
    }

    @Override
    public BigDecimal getAllPay(int userId) {
        return getBaseMapper().getAllPay(userId);
    }

    @Override
    public BigDecimal getAllPayByType(int userId, int price) {
        BigDecimal pay = getBaseMapper().getPayByType(userId, price + "");
        return pay == null ? BigDecimal.ZERO : pay;
    }

    @Override
    public UserDailyLuckRecord getRecordByToday(Integer userId, Integer roomId, String poolType, String type, Integer poolId){
        String date = StrToMillsUtil.millsToStrDate(new Date(), null);
        return getOne(new QueryWrapper<UserDailyLuckRecord>()
                .eq("user_id", userId)
                .eq("room_id", roomId)
                .eq("pool_type", poolType)
                .eq("pool_id", poolId)
                .eq("type", type)
                .eq("luck_date", date)
        );
    }

    @Override
    public HashMap<String, BigDecimal> getLuckByType(Integer userId, Integer price) {
        HashMap<String, BigDecimal> map = getBaseMapper().getLuckByType(userId, price + "");
        if (map==null) {
            map = new HashMap<>();
            map.put("pay", BigDecimal.ZERO);
            map.put("income", BigDecimal.ZERO);
        }
        return map;
    }

    @Override
    public List<UserDailyLuckRecord> getRecordDate(Integer price) {
        return getBaseMapper().getRecordDate(price);
    }

    @Override
    public HashMap<String, BigDecimal> getLuckByUser(Integer userId) {
        HashMap<String, BigDecimal> map = getBaseMapper().getLuckByUser(userId);
        if (map==null) {
            map = new HashMap<>();
            map.put("pay", BigDecimal.ZERO);
            map.put("income", BigDecimal.ZERO);
        }
        return map;
    }

    @Override
    public HashMap<String, BigDecimal> getLuckByRoomId(Integer roomId) {
        HashMap<String, BigDecimal> map = getBaseMapper().getLuckByRoomId(roomId);
        if (map==null) {
            map = new HashMap<>();
            map.put("pay", BigDecimal.ZERO);
            map.put("income", BigDecimal.ZERO);
        }
        return map;
    }

    @Override
    public HashMap<String, BigDecimal> getLuckByIp(String ip) {
        HashMap<String, BigDecimal> map = getBaseMapper().getLuckByIp(ip);
        if (map==null) {
            map = new HashMap<>();
            map.put("pay", BigDecimal.ZERO);
            map.put("income", BigDecimal.ZERO);
        }
        return map;
    }

    @Override
    public HashMap<String, BigDecimal> getLuckByDevice(String device) {
        HashMap<String, BigDecimal> map = getBaseMapper().getLuckByDevice(device);
        if (map==null) {
            map = new HashMap<>();
            map.put("pay", BigDecimal.ZERO);
            map.put("income", BigDecimal.ZERO);
        }
        return map;
    }
}